<template>
  <div class="container">
    <Navbar title="项目详情" foo-back @back="goBack" />
    <div class="content">
      <!-- <div class="swiper-wrap" v-if="imgUrls.length==0"><img src="@/assets/project/noProject.png" />   </div> -->
      <div style="background:#fff">
      <div class="swiper-wrap">
        <div v-if="imgUrls.length==0" style="width:100%;height:160px;text-align:center">
          <img src="@/assets/project/noProject.png" alt="暂无图片" />
          <div style="margin-top:-10px">暂无图片</div>
        </div>
        <van-swipe
          @change="onChange2"
          class="back-swipe"
          :autoplay="3000"
          show-indicators
          :height="160"
          v-else
        >
          <van-swipe-item v-for="(image, index) in imgUrls" :key="index">
            <img :src="image" />
          </van-swipe-item>
          <template #indicator>
            <div class="custom-indicator">{{ current + 1 }}/{{imgUrls.length}}</div>
          </template>
        </van-swipe>
      </div>
      </div>

      <div style="margin:-115px 0 0 20px;position:absolute" @click="goBack()" v-if="false">
        <van-icon size="16px" name="arrow-left" />
      </div>
      <div class="desc-content">
        <div class="title-text">
          <p class="title">{{projectDetail.projectName}}</p>
          <div class="info flex" style="justify-content: space-between;">
            <div>
              <p class="desc" v-if="projectDetail.cityProjectTypeDesc">{{projectDetail.cityProjectTypeDesc}}</p>
              <p class="desc" v-if="projectDetail.provinceProjectTypeDesc">{{projectDetail.provinceProjectTypeDesc}}</p> 
            </div>
            <div class="img"></div>
          </div>
          <!-- 投资方介绍 -->
          <div class="project-detail-content detail-content-wrapper">
            <div class="info-bottom">
              <div class="flex">
                <P class="title t-width">投资方</P>
                <span class="text">{{projectDetail.investDept || '-'}}</span>
              </div>
              <div class="flex" style="margin-top:2px;line-height:24px">
                <p class="title t-width">项目总投资</p>
                <span class="num">{{projectDetail.projectTotalInvest || '-'}}</span>
                <span
                  style="margin-left:2px"
                  class="num"
                >{{projectDetail.projectTotalInvestUnit=='1'?'元':projectDetail.projectTotalInvestUnit=='2'?'万元':projectDetail.projectTotalInvestUnit=='3'?'亿':''}}</span>
              </div>
            </div>
          </div>
          <!-- 项目建设内容 -->
          <div class="project-detail-content">
            <div class="info-bottom">
              <p class="title">项目建设内容</p>
              <p class="desc-text">{{projectDetail.projectConstructDesc || '-'}}</p>
            </div>
          </div>
          <!-- 进展情况 -->
          <div class="project-detail-content">
            <div class="info-bottom">
              <p class="title">当前进展</p>
              <p class="desc-text">{{projectDetail.currentProgressDesc || '-'}}</p>
            </div>
          </div>
          <div class="zhanwei"></div>
        </div>
      </div>
    </div>
    <div class="btn-content">
      <div class="images">
        <div class="attention" @click="btnClick()">
          <img :src="isFocus?focus:unFocus" />
          <div class="btn-name">{{isFocus?'已关注':'关注'}}</div>
        </div>
        <div class="link" @click.stop="link()">
          <img src="@/assets/project/telephone.png" alt />
          <div class="btn-name">联系责任人</div>
        </div>
        <div class="navigation" @click.stop="openNavigationList">
          <img src="@/assets/project/navigation.png" alt />
          <div class="nav-btn">导航</div>
        </div>
      </div>
    </div>
    <div class="navigationList" v-if="navigationListIsShow" @click.stop>
      <p @click.stop="navigation('bd')">百度地图</p>
      <p @click.stop="navigation('gd')">高德地图</p>
      <p @click.stop="navigationListIsShow = false">取消</p>
    </div>
    <!-- <div class="link-pop" v-if="linkShow" @click.stop>
      <p @click.stop><a @click.stop="handleSendClick">发消息</a></p>
      <p @click.stop><a :href="telLink">打电话</a></p>
      <p @click.stop="navigation('gd')">视频会议</p>
      <p @click.stop="linkShow = false">取消</p>
    </div> -->
     <van-action-sheet
      z-index="100"
      :show="linkShow"
      :actions="actions"
      cancel-text="取消"
      @select="onSelect"
      close-on-click-action
      safe-area-inset-bottom
      @cancel="linkShow = false"
      @click-overlay="linkShow = false"
    />
    <div class="van-safe-area-top"></div>
    <!-- <van-loading v-if="myLocationStatus" vertical color="#1989fa">正在获取定位</van-loading> -->
    <van-overlay :show="navigationListIsShow" @click.stop="navigationListIsShow = false" />
  </div>
</template>

<script lang="ts" setup>
import { computed, ref, reactive, toRefs, watch, onActivated, onMounted } from 'vue'
import { showImagePreview, showToast, showFailToast } from 'vant'
import { useRouter } from 'vue-router'
import ProjectServe from '@/api/keyProject'
import focus from '@/assets/project/attentioned.png'
import unFocus from '@/assets/project/detailattention.png'
import ContactServe from '@/api/contact.ts'
import { getVideoUrl } from '@/utils/index'
import { storeToRefs } from 'pinia'
import { useUserStore } from '@/store'

const router = useRouter()

defineOptions({
  name: 'projectDetail'
})
const props = defineProps({
  detailData: {
    type: Object,
    default: {}
  }
  //   activeTab: {
  //   type: Number,
  //   default: 0
  // },
})
// 取出用户信息
const userStore = useUserStore()
const { userInfo } = storeToRefs(userStore)
const projectDetail = ref({})
const defaultImg = ref('src/assets/project/default.png')
const route = useRouter()
const isFocus = ref()
const imgUrls = ref<RecordType[]>([])
const current = ref(0)
const currentTab = ref(0)
const navigationListIsShow = ref(false)
const linkShow = ref(false)
const myLocation = reactive({
  lng: <any>null,
  lat: <any>null,
  city: null
})
const myLocationStatus = ref(false)
watch()
// () => props.detailData,
// val => {
//   projectDetail.value = val;
//   isFocus.value = projectDetail.value.focusNot == '1' ? true : false;
//   imgUrls.value = projectDetail.value.imageUrls?projectDetail.value.imageUrls:[];
// }
const actions = [
      { name: '发消息',className:'link-line'},
      { name: '打电话' },
      { name: '视频会议' }
    ];

const onSelect = (item: any) => {
      // 默认情况下点击选项时不会自动收起
      // 可以通过 close-on-click-action 属性开启自动收起
  if(item.name == '发消息'){
    handleSendClick()
  }
  if(item.name == '打电话'){
    handleCallClick()
  }
  if(item.name == '视频会议'){
    handleMeeting()
  }
  linkShow.value = false
 };

const initial = computed(() => {
  if (userInfo.value) return userInfo.value.realName.slice(0, 1)
  return ''
})

onMounted(() => {
  navigatorGeolocation()
  // 路由参数
  projectDetail.value = route.currentRoute.value.query
  isFocus.value = projectDetail.value.focusNot == '1' ? true : false
  imgUrls.value =
    projectDetail.value.imageUrls && projectDetail.value.imageUrls.length > 0
      ? projectDetail.value.imageUrls
      : []
})
const goBack = () => {
  route.push({
    path: '/project',
    query: { current: 'detail' }
  })
  // route.back()
}
const onChange2 = index => {
  current.value = index
}

// 关注事件
const btnClick = () => {
  let param = {
    productId: projectDetail.value.projectId,
    projectName: projectDetail.value.projectName
  }
  //如果已关注，则取消关注
  if (isFocus.value) {
    ProjectServe.cancelFocus(param).then(res => {
      console.log('cancelFocus****' + res.data)
      if (res.status == '1') {
        //点击取消关注
        showToast('取消关注成功')
        isFocus.value = false
        // initProjectData();
      }
    })
  } else {
    ProjectServe.focus(param).then(res => {
      console.log('focus****' + res.data)
      if (res.status == '1') {
        //点击关注后，按钮切换为已关注
        showToast('关注成功')
        isFocus.value = true
      }
    })
  }
}
const navigatorGeolocation = () => {
  console.log('获取当前定位')
  // 获取当前定位
  if (navigator.geolocation) {
    navigator.geolocation.getCurrentPosition(
      //success
      function (position) {
        myLocation.lng = position.coords.longitude
        myLocation.lat = position.coords.latitude
        let e = {
          lnglat: { lng: position.coords.longitude, lat: position.coords.latitude }
        }
        myLocationStatus.value = true
        navigationListIsShow.value = false
      },
      //error
      function (error) {
        myLocationStatus.value = false
        navigationListIsShow.value = false
        switch (error.code) {
          case error.PERMISSION_DENIED:
            console.log('用户拒绝对获取地理位置的请求。')
            break
          case error.POSITION_UNAVAILABLE:
            console.log('位置信息是不可用的。')
            break
          case error.TIMEOUT:
            console.log('请求用户地理位置超时。')
            break
          case error.UNKNOWN_ERROR:
            console.log('未知错误。')
            break
        }
        // showFailToast('定位失败，请检查定位权限')
      },
      //options
      {
        timeout: 5000
      }
    )
  } else {
    console.log('不支持获取地理位置。')
  }
}
const openNavigationList = () => {
  if (myLocationStatus.value) {
    navigationListIsShow.value = true
  } else {
    showFailToast('定位失败，请检查定位权限')
  }
}

// 联系责任人弹框
const link = () => {
  console.log('打开联系人弹框')
  linkShow.value = true;
}
// 发消息
const handleSendClick = () => {
  router.push({
    path: '/contact/talk',
    query: {
      id: projectDetail.value.personLiableId?.personLiableId,
      initial: projectDetail.value.personLiableName?.personLiableName
    }
  })
}
//打电话
const handleCallClick = () => {
  if (projectDetail.value.personLiableNumber){
    window.location.href = 'tel://' + projectDetail.value.personLiableNumber;
  } else {
    showFailToast('对不起，该项目责任人未绑定联系方式！')
  }
}
// 打电话
const telLink = computed(() => {
  if (projectDetail.value) return 'tel://' + projectDetail.value.personLiableNumber  //项目联系人电话
  return ''
})
// 视频会议
const handleMeeting = () => {
  const url = getVideoUrl();
  window.open(url, '_blank')
}
const navigation = mapType => {
  console.log('myLocation', myLocation)
  let destination = projectDetail.value
  console.log('destination', destination)
  if (!myLocation || !myLocation.lng || !myLocation.lat) {
    return
  }
  let Url = ''
  switch (mapType) {
    case 'gd': //高德地图
      Url = `https://uri.amap.com/navigation?from=${myLocation.lng},${myLocation.lat},起点&to=${destination.longitude},${destination.latitude},${destination.projectName}&coordinate=wgs84`
      break
    case 'bd': //百度地图
      Url = `https://api.map.baidu.com/direction?origin=latlng:${myLocation.lat},${myLocation.lng}|name:起点&destination=latlng:${destination.latitude},${destination.longitude}|终点:${destination.projectName}&mode=driving&region=${myLocation.city}&coord_type=wgs84&output=html&src=webapp.baidu.openAPIdemo`
      break
  }
  window.open(Url, '_self')
  // location.href = Url;
}
</script>

<script lang="ts">
</script>

<style lang="less" scoped>
.container {
  // background: #fff0e7;
  height: 100vh;
  --van-action-sheet-item-line-height:40px;
  background: linear-gradient(to bottom, rgba(255, 240, 231, 1) 0%, rgba(255, 240, 231, 1) 50%, #fff 80%, #fff 100%);
  .link-line {
    border: 1px solid red;
  }
  .content {
    position: relative;
    width: 100%;
    height: calc(100% - 123px);
    overflow-y: scroll;
    .image-content {
      width: 100%;
      height: 166px;
    }
    .desc-content {
      .title-text {
        height: 106px;
        padding: 18px 16px;
        background: url(@/assets/project/project-background.png);
        background-size: cover;
        background-repeat: no-repeat;
        .title {
          color: #0e0d16;
          font-weight: bold;
          font-size: 18px;
          line-height: 23px;
        }
        .t-width {
          width: 100px;
        }
        .info {
          // padding:11px 0;
          height: 50px;
          line-height: 50px;
          .desc {
            color: #ff8326;
            font-size: 11px;
            font-family: 'PingFang SC';
            background-color: rgba(255, 131, 38, 0.1);
            display: inline-block;
            padding: 0 10px;
            line-height: 25px;
            border-radius: 5px;
            height: 25px;
            margin-top: 15px;
            margin-right: 10px;
          }
          .img {
            width: 120px;
            margin-top: -20px;
            // height: 60px;
            background: url(@/assets/project/project.png);
            background-size: 120px 110px;
            background-repeat: no-repeat;
          }
        }
      }
      .project-detail-content {
        border-radius: 10px;
        background: #ffffff;
        color: #212121;
        box-shadow: 0px 3px 5px 0px #00000014;
        margin-top: 10px;
        margin-left: -6px;
        width: 318px;
        padding: 16px 16px;
        // height: 89px;
        overflow: auto;
        .info-bottom {
          font-size: 14px;
          font-family: 'PingFang SC';
          color: #626262;
          .title {
            font-weight: bold;
            font-size: 14px;
            line-height: 24px;
          }
          .text {
            color: #0e0d16;
            font-size: 14px;
            line-height: 20px;
            width:calc(100% - 100px)
          }
          .num {
            font-size: 14px;
            font-weight: bold;
            line-height: 24px;
            color: #ff8326;
          }
          .desc-text {
            color: #0e0d16;
            font-size: 13px;
            line-height: 20px;
            margin-top: 6px;
          }
        }
      }
      
      .zhanwei{
        height: 30px;
      }

      .detail-content-wrapper {
        margin-top: 0px;
        // height: 89px;
      }
    }
  }
  .btn-content {
    position: fixed;
    bottom: 0px;
    // background: #ffffff;
    color: #212121;
    // box-shadow: 0px 3px 5px 0px #00000014;
    width: 100%;
    height: 63px;
    line-height: 63px;
    display: flex;
    justify-content: center;
    justify-items: center;
    
    .images {
      margin-top: 15px;
      display: flex;
      width: 100%;
      height: 32px;
      align-items: center;
      justify-content: center;
      // flex-wrap: wrap;
      .attention {
        width: 100px;
        height: 32px;
        border-radius: 18px;
        font-size: 12px;
        line-height: 32px;
        // border: 0.5px solid #ff8326;
        border-radius: 25px;
        border: 0.5px solid #3A75C5;
        box-shadow: 0px 2px 5px 0px #00000019;
        display: flex;
        font-family: 'PingFang SC';
        justify-content: center;
        align-items: center;
      }
      .link {
        width: 131px;
        height: 32px;
        border-radius: 18px;
        font-size: 12px;
        line-height: 32px;
        // border: 0.5px solid #ff8326;
        margin: 0 0 0 8px;
        border-radius: 25px;
        border: 0.5px solid #3A75C5;
        box-shadow: 0px 2px 5px 0px #00000019;
        display: flex;
        font-family: 'PingFang SC';
        justify-content: center;
        align-items: center;
      }
      .navigation {
        width: 100px;
        height: 32px;
        border-radius: 18px;
        font-size: 12px;
        line-height: 32px;
        // border: 0.5px solid #ff8326;
        display: flex;
        font-family: 'PingFang SC';
        background: #3A75C5;
        color: #fff;
        margin: 0 0 0 8px;
        justify-content: center;
        align-items: center;
        border-radius: 25px;
        background: #3A75C5;
        box-shadow: 0px 4px 10px 0px #00000019;
        // .navigation-img {
        //   background: url('@/assets/project/navigation.png') no-repeat center;
        //   background-size: 15px 15px;
        //   padding-left: 40px;
        // }
      }
      img {
        // border-radius: 5px;
        width: 15px;
        height: 15px;
        margin: 0 2px 0 0;
        &:nth-child(4n) {
          margin-right: 0;
        }
      }
      .nav-btn {
        font-size: 14px;
      }
      .btn-name {
        color: #3A75C5;
        font-size: 14px;
        // margin-left: -10px;
      }
    }
  }
}
.navigationList {
  position: absolute;
  bottom: 0;
  left: 50%;
  transform: translateX(-50%);
  width: 100%;
  text-align: center;
  padding: 0 10px 6px;
  background-color: #fff;
  border-radius: 5% 5% 0 0;
  z-index: 1300;
  p {
    padding: 10px;
    color: #1937d8;
  }
  p:nth-child(2) {
    border-top: 1px solid #ccc;
    border-bottom: 1px solid #E2E2E2;
  }
}
.link-pop {
  position: absolute;
  bottom: 0;
  left: 50%;
  transform: translateX(-50%);
  width: 100%;
  text-align: center;
  padding: 10px 0 0 0;
  background-color: #fff;
  border-radius: 5% 5% 0 0;
  z-index: 1300;
  p {
    padding: 20px;
    color: #1937d8;
    border-bottom: 1px solid #E2E2E2;
  }
  p:nth-child(4)  {
    // border-top: 1px solid #ccc;
    border-bottom: none;
  }

}

.custom-indicator {
  position: absolute;
  right: 5px;
  bottom: 5px;
  padding: 4px 8px;
  border-radius: 8px;
  color: #ffffff;
  font-size: 12px;
  background: rgba(0, 0, 0, 0.4);
}
.swiper-wrap {
  width: 100%;
  //  height: 160px;
  background-color: #fff;
  -webkit-mask-image: linear-gradient(to top, rgba(255, 255, 255, 1), rgba(255, 255, 255, 0));
  mask-image: linear-gradient(to top, rgba(255, 255, 255, 1), rgba(255, 255, 255, 0));
  //  opacity: 0.5;
  transition: opacity 0.5s ease-in-out; /*添加渐变效果*/
  .back-swipe {
    img {
      width: 100%;
      height: 160px;
      // -webkit-background-image: linear-gradient(
      //   to bottom,
      //   rgba(255, 255, 255, 1) 0%,
      //   rgba(255, 255, 255, 0) 50%
      // );
      // background-image: linear-gradient(
      //   to bottom,
      //   rgba(255, 255, 255, 1) 0%,
      //   rgba(255, 255, 255, 0) 50%
      // );
      // opacity: 0.5;
      /*把图片的透明度设置为0.6*/
      // transition: opacity 0.5s ease-in-out;  /*添加渐变效果*/
    }
  }
  img {
    width: 123px;
    height: 106px;
    // margin-top:-10px;
  }
}
.van-loading {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translateX(-50%);
  z-index: 1010;
  background-color: #fff;
  padding: 20px;
  border-radius: 5%;
}
</style>

